package cn.demoncat.util.mybatis.plus.method;

import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;

/**
 * 清空表 TRUNCATE TABLE
 *
 * @author 延晓磊
 * @since 2021年08月31日
 */
public class Truncate extends AbstractMethod {

    /**
     * Mapper方法名
     */
    public static final String NAME = "truncate";
    /**
     * SQL脚本
     */
    private static final String SQL = "<script>\nTRUNCATE TABLE %s\n</script>";

    public Truncate() {
        super(NAME);
    }

    /**
     * 自定义Statement
     *
     * @param mapperClass
     * @param modelClass
     * @param tableInfo
     * @return
     */
    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        // 自定义SQL
        String sql = String.format(SQL, tableInfo.getTableName());
        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
        return this.addDeleteMappedStatement(mapperClass, this.methodName, sqlSource);
    }

}